package com.turing.post.tuxedo.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.transaction.util.MchAddTradDetail;
import com.turing.post.transaction.util.MrchBusiness;
import com.turing.post.transaction.util.MrchTransParam;
import com.turing.post.tuxedo.util.CardPaidObj;
import com.turing.post.tuxedo.util.IPaid;
import com.turing.post.tuxedo.util.LocalService;
import com.turing.post.tuxedo.util.LocalServiceImpl;
import com.turing.post.tuxedo.util.OPaid;
import com.turing.post.tuxedo.util.PCPSService;
import com.turing.post.tuxedo.util.BusinessDetailObj;
import com.turing.post.tuxedo.util.TransRequest;
import com.turing.post.tuxedo.util.TransResponse;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "PCPS.SVC9010")
@Remote({ LocalService.class, PCPSService.class })
@Local
public class Paid extends LocalServiceImpl implements LocalService, PCPSService {

	@EJB(mappedName = "transaction.impl.MrchBusinessImpl")
	private MrchBusiness mrchBusinessImpl;

	/**
	 * generate IPaid
	 * 
	 * @return IPaid
	 */
	public IPaid makeRequestObject() {
		return new IPaid();
	}

	/**
	 * Paid 数据检查
	 * 
	 * @param req
	 * @param ack
	 */
	protected void preService(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {
	}

	/**
	 * Paid service entry 预付卡支付的业务逻辑
	 * 
	 * @param req
	 * @param ack
	 */
	protected void Service(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {

		IPaid itr = (IPaid) req;
		OPaid otr = (OPaid) ack;

		// 设置基本参数
		MrchTransParam imtp = setMrchTransParam(req);

		// 交易摘要(可选)
		imtp.mrchAbstract = itr.additionalObj.asbtract;

		// 会员卡号
		imtp.membershipCardNo = itr.memberShipObj.cardObj.cardNo;
		// 会员卡支付密码
		imtp.membershipCardPwd = itr.memberShipObj.passwd;
		// 会员卡支付额
		imtp.scorePaidAmount = itr.memberShipObj.scorePaidAmount;
		// 其他支付发生额
		imtp.otherPaidAmount = itr.otherPaidAmount;
		// 卡合计发生额
		imtp.totalAmount = itr.paidObj.totalAmount;

		// 卡支付MAP
		if (imtp.cardAccMap != null)
			imtp.cardAccMap.clear();
		else
			imtp.cardAccMap = new HashMap<String, MrchTransParam.CardAccount>();
		for (CardPaidObj cpo : itr.paidObj.cplist) {
			MrchTransParam.CardAccount mca = imtp.new CardAccount();
			// 验证卡信息的完整性
			if (!PCPSUtil.isEmpty(cpo.cardObj.track2))
				cpo.cardObj.cardNo = mrchBusinessImpl.getValidateCardNo(
						req.merchantNo, cpo.cardObj.cardNo, cpo.cardObj.track2,
						cpo.cardObj.track3);
			mca.cardNo = cpo.cardObj.cardNo;
			mca.track2 = cpo.cardObj.track2;
			mca.track3 = cpo.cardObj.track3;
			mca.cardPwd = cpo.passwd;
			mca.amount = cpo.amount;
			imtp.cardAccMap.put(mca.cardNo, mca);
		}

		// 附加信息
		if (imtp.matdlist != null)
			imtp.matdlist.clear();
		else
			imtp.matdlist = new ArrayList<MchAddTradDetail>();

		if (itr.additionalObj != null && itr.additionalObj.saleOrderObj != null
				&& itr.additionalObj.saleOrderObj.businessDetailObjs != null) {
			for (BusinessDetailObj pdo : itr.additionalObj.saleOrderObj.businessDetailObjs) {
				MchAddTradDetail matd = new MchAddTradDetail();
				matd.setMrchProdId(pdo.businessType);
				matd.setMrchProdName(pdo.businessType);
				matd.setMrchProdPrice(pdo.businessAmount);
				matd.setMrchProdQuantity(BigInteger.ONE);
				matd.setMrchProdAmount(pdo.businessAmount);
				matd.setMrchProdCost(pdo.businessAmount);
				matd.setMrchProdFactSale(pdo.businessAmount);
				matd.setMrchProdProfitMCH(BigInteger.ZERO);
				matd.setMrchProdProfitSYS(BigInteger.ZERO);
				imtp.matdlist.add(matd);
			}
		}

		// 代理操作员号
		imtp.OperId = null;

		// 执行交易
		MrchTransParam omtp = mrchBusinessImpl.Paid(imtp);

		// 主机流水号
		otr.ackSeqNo = omtp.tranSeqNo;
		otr.tranTime = itr.curSysDateTime;
		otr.respCode = TuxedoRespCode.RC_SUCCESS;
		otr.factScorePaidAmount = omtp.factScorePaidAmount;
		otr.score = omtp.score;
		otr.scoreAmount = omtp.scoreAmount;
		otr.totalFee = BigInteger.ZERO;

		otr.cplist = itr.paidObj.cplist;

		// 设置返回余额字段
		otr.cardNum = BigInteger.valueOf(omtp.cardAccMap.size());
		otr.allCard = new HashMap<String, BigInteger>();
		for (Map.Entry<String, MrchTransParam.CardAccount> mca : omtp.cardAccMap
				.entrySet()) {
			otr.allCard.put(mca.getKey(), mca.getValue().balance);
			otr.allFee.put(mca.getKey(), mca.getValue().fee);
			otr.totalFee = otr.totalFee.add(mca.getValue().fee);
		}
	}

	/**
	 * Paid 服务入口
	 * 
	 * @param req
	 * @return TransResponse
	 */
	public TransResponse Service(TransRequest req)
			throws PCPSApplicationException {
		// 按普通方法执行服务
		return CommonService(req, new OPaid());
	}
}